package com.ctim.web.util.oss;

import com.aliyun.oss.OSSClient;
import org.apache.log4j.Logger;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.ExecutionException;

public class OSSUploadFile {

    public static final Logger LOGGER = Logger.getLogger(OSSUploadFile.class);

    private OSSClient client;//ossclient
    private File file;//上传文件
    private BufferedInputStream sourceStream;//上传流
    private String bucketName;//bucketName
    private String key;//云端存储路径
    /**
     * oss上传 支持断点续传
     * @param file    源文件
     * @param bucketName    bucketName
     * @param key           存储key  -在oss的存储路径
     */
    public OSSUploadFile(File file, String bucketName, String key) {
        this.file = file;
        this.bucketName = bucketName;
        this.key = key;
    }

    /**
     * oss上传 支持断点续传
     * @param io    源文件路径
     * @param bucketName    bucketName
     * @param key           存储key  -在oss的存储路径
     */
    public OSSUploadFile(BufferedInputStream io, String bucketName, String key) {
        //实例化单文件上次线程池
        this.sourceStream = io;
        this.bucketName = bucketName;
        this.key = key;
    }

    /**
     * oss上传
     * @return int
     */
    public String uploadFile() throws ExecutionException, InterruptedException {
        String result;
        client  = OSSClientFactory.getInstance();
        // 准备Bucket
        if (sourceStream != null) {
            result = putObjectByStream(client, bucketName, key);
        } else {
            result = putObjectByFile(client, bucketName, key);
        }
//            client.shutdown();
        return result;
    }


    public static File inputstreamtofile(InputStream ins,String fileName) throws IOException {
        File file = new File("D:/test/"+fileName);
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

    private String putObjectByFile(OSSClient ossClient,String bucketName,String key) {
        if (file.exists()){
            ossClient.putObject(bucketName, key, file);
            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
            // 生成URL
            URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
            if (url != null) {
                return url.toString();
            }
        }
        return "";
    }

    private String putObjectByStream(OSSClient ossClient,String bucketName,String key) {
        ossClient.putObject(bucketName, key, this.sourceStream);
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        // 生成URL
        URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
        if (url != null) {
            return url.toString();
        }
        return "";
    }
}